package cn.e4j.modular.crm.controller.bb;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.modular.api.service.BftjService;
import cn.e4j.modular.api.service.XsfxService;
import cn.e4j.modular.api.util.ControllerSupport;
import cn.e4j.modular.system.entity.SysUser;
import cn.e4j.modular.system.service.impl.SystemServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 拜访统计
 */
@Controller
@RequestMapping("/bb/bftj")
public class BbbftjController extends ControllerSupport {

    @Autowired
    SystemServiceImpl systemService;

    @Autowired
    BftjService bftjService;


    /**
     * 跳转页面
     */
    @RequiresPermissions("bb:bftj:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/bb/bftj.html";
    }



    @RequiresPermissions("bb:bftj:init")
    @ResponseBody
    @RequestMapping(value = "getList")
    public ResponseData getList(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        List<Dto> result = new ArrayList<>();

        try {
            SysUser currentUser = new SysUser();
            currentUser.setID(shiroUser.getID());
            currentUser.setORG_ID(shiroUser.getORG_ID());
            inDto.put("USERID", currentUser.getID());

            //上下级权限
            Dto sys003 = systemService.getSys003(currentUser);
            List<Dto> listOrg = (List<Dto>) sys003.get("ORGS");
            inDto.put("ORGS", listOrg);

            Dto sys004 = systemService.getSys004(currentUser, listOrg);
            inDto.put("YWY",sys004.get("YWY"));

            result = bftjService.getList(inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS(result);
    }

    @RequestMapping(value = "getInfo")
    @ResponseBody
    public ResponseData getInfo(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> result = new ArrayList<>();
        try {
            result = bftjService.getInfo(inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS(result);
    }

}
